/**
 * @Author: lusx
 * @Date:   05-03-2019 06:39:43
 * @Project: Govern督办
 * @Last modified by:   lusx
 * @Last modified time: 29-03-2019 11:33:41
 */
import { observable, action,toJS } from "mobx";
import { ShareAuth, Form ,ListViewPage,BrowserHrm} from "weaver-mobile-page";
import { Bubble,Toast} from 'weaver-mobile';
import heartStore from "./agent/heartStore.js";
import selectTime from "./data/selectTimeStore.js";
import _ from "lodash";
import { Base64 } from "js-base64";
import ComShare from "../components/governTools/comShare/comShare.js";
export class SupStore {
  @observable withSwipeAction = true;
  @observable shareAuthRoute = new ShareAuth.Store({
    route: "/share",
    id: "list"
  });
  @observable gform = new Form.Store({ route: "/govern" });
  @observable list = new ListViewPage.Store({ route: '/governList' ,id:'sharelist'});
  @observable browserHrm = new BrowserHrm.Store({ route: '/infoSearch', id: 'mulHrm' });
  @observable reporter = "";//成文等搜索,人力资源浏览按钮值
  @observable inProgress = false;
  @observable shareCondition = [];
  @observable selectOptions = selectTime.Options;//成文等搜索,时间选择框数据源
  @observable reportDate_select = "";//成文等搜索,时间选择框key值
  @observable reportDate_start = "";//成文等搜索,时间选择框
  @observable reportDate_end = "";//成文等搜索,时间选择框

  shareSubData = {};
  richEValue = {};
  sliderValue = 0;
  constructor(props) {
    //super(props)
    this.setShareSubData = this.setShareSubData.bind(this);
    this.setRichEValue = this.setRichEValue.bind(this);
    this.setSliderValue = this.setSliderValue.bind(this);
    this.setReporter = this.setReporter.bind(this);
    this.setReportDate_select = this.setReportDate_select.bind(this);
    this.sertReportDate = this.sertReportDate.bind(this);
    this.clearInfoSearch = this.clearInfoSearch.bind(this);
    this.loadingToast = this.loadingToast.bind(this);
  }
  //我的关注
  @action
  setHeart(params) {
    heartStore.setHeart();
  }
  //新的form
  @action
  newGform(){
    this.gform = new Form.Store({ route: "/govern" });
  }
  //表单初始化方法
  @action
  initGForm(condition, route) {
    this.gform = new Form.Store({ route });
    this.gform.initForm(condition, route);
  }
  //重置表单的值为空
  @action
  resetFields(){
    this.gform.resetFields();
  }
  //表单校验
  @action
  doValidate(){
    let validResult = this.gform.doValidate();
    if (validResult.pass) {
      return true;
    }else {
      Bubble.error(validResult.message);
      return false;
    }
  }
  //获取共享键值对
  setShareSubData(data) {
    this.shareSubData = {};
    data = toJS(data);
    try{
      if (data.sharetype === "6") {
        this.shareSubData = {
          sharetype: data.sharetype,
          sharetypespan: data.showname,
          joblevel: data.joblevel.joblevel,
          opttype: data.opttype,
          relatedid: data.relatedid,
          relatedidspan: data.relatedidspan,
        }
        let jobleveltextsubcompany = "";
        let jobleveltextsubcompanyspan = "";
        let jobleveltextdepartment = "";
        let jobleveltextdepartmentspan = "";
        if (data.joblevel) {
          if (data.joblevel.jobleveltextsubcompany) {
            data.joblevel.jobleveltextsubcompany.forEach(element => {
              if (jobleveltextsubcompany === "") {
                jobleveltextsubcompany = element.id;
                jobleveltextsubcompanyspan = element.name;
              } else {
                jobleveltextsubcompany = jobleveltextsubcompany + "," + element.id;
                jobleveltextsubcompanyspan = jobleveltextsubcompanyspan + "," + element.name;
              }
            })
            this.shareSubData = {
              ...this.shareSubData,
              jobleveltextsubcompany,
              jobleveltextsubcompanyspan
            }
          }
          if (data.joblevel.jobleveltextdepartment) {
            data.joblevel.jobleveltextdepartment.forEach(element => {
              if (jobleveltextdepartment === "") {
                jobleveltextdepartment = element.id;
                jobleveltextdepartmentspan = element.name;
              } else {
                jobleveltextdepartment = jobleveltextdepartment + "," + element.id;
                jobleveltextdepartmentspan = jobleveltextdepartmentspan + "," + element.name;
              }
            })
            this.shareSubData = {
              ...this.shareSubData,
              jobleveltextdepartment,
              jobleveltextdepartmentspan
            }
          }
        }
        
      }else{
        if(Object.keys(data).length>0){
          this.shareSubData = _.mapKeys(data, function(value, key) {
            if(key === 'showname'){
              return 'sharetypespan'
            }else{
              return key
            }
          });
        }else{
          throw "data has error";
        }
      }
    }catch(error){
      console.error(error);
    }


  }
  //获取富文本内容
  setRichEValue(data,comForm) {
    Object.assign(this.richEValue,data);
    let datas =  {};
    Object.keys(data).forEach(key=>{
      Object.assign(datas,{[key]:{value:data[key]}})
    }) 
    if(comForm){
      comForm.updateFields(datas);

    }
  }

 
  _setRichEValue = (condition) => {
    condition[0].items.map((item)=>{
      if(item.formItemType === "RICHTEXT"){
        this.setRichEValue({[item.domkey[0]]:Base64.decode(item.value)});
      }
    })
  }
  getFormData = () => {
    let formData = {};
    Object.keys(this.richEValue).forEach(key=>{
      Object.assign(formData,{[key]:{value:this.richEValue[key]}})
    }) 
    return formData;
  }

  //获取进度
  setSliderValue(data){
    this.sliderValue = data;
  }
  //设置人力资源浏览框值 通用于成文等的搜索页面
  @action
  setReporter(params){
    this.reporter = params;
  }
  //通用于成文等的搜索页面
  @action
  setReportDate_select(params){
    this.reportDate_select = params;
  }
  //通用于成文等的搜索页面
  @action
  sertReportDate(params){
    this.reportDate_start = params[0];
    this.reportDate_end = params[1];
  }
  //加载中
  loadingToast(isloading){
    Toast.hide();
    isloading ? Toast.loading('Loading...',0): Toast.hide();
  }
  //清空详情(info)页面搜索条件
  clearInfoSearch(){
    this.reporter = "";
    this.reportDate_select = "";
    this.reportDate_start = "";
    this.reportDate_end = "";
  }
}
